#ifndef FOC_RUN_H
#define FOC_RUN_H

#include "main.h"
#include "RS485.h"
#include "IQ_math.h"

//#define EN_GATE_SET				HAL_GPIO_WritePin(EN_GATE_GPIO_Port, \
//									EN_GATE_Pin, GPIO_PIN_SET)
//#define EN_GATE_RESET			HAL_GPIO_WritePin(EN_GATE_GPIO_Port, \
//									EN_GATE_Pin, GPIO_PIN_RESET)

#define LED_DRV_SET				HAL_GPIO_WritePin(LED_GPIO_Port, \
									LED_Pin, GPIO_PIN_SET)
#define LED_DRV_RESET			HAL_GPIO_WritePin(LED_GPIO_Port, \
									LED_Pin, GPIO_PIN_RESET)

//#define DISCHARGE_MOS_SET		HAL_GPIO_WritePin(DISCHARGE_MOS_GPIO_Port, \
//									DISCHARGE_MOS_Pin, GPIO_PIN_SET)
//#define DISCHARGE_MOS_RESET		HAL_GPIO_WritePin(DISCHARGE_MOS_GPIO_Port, \
//									DISCHARGE_MOS_Pin, GPIO_PIN_RESET)

//#define READ_NFAULT				HAL_GPIO_ReadPin(nFAULT_GPIO_Port, \
//									nFAULT_Pin)
#define READ_NOCTW				HAL_GPIO_ReadPin(GPIOB, \
									GPIO_PIN_12)
									
#define PWM_PERIOD			 ((uint16_t)7200)
#define PWM_ADC_TRGO			 ((uint16_t)7200-1-22)
#define PWM_HalfPerMax   ((uint16_t)PWM_PERIOD/2)

#define PWM_FREQ  (SystemCoreClock)/(2*PWM_PERIOD)       //10KHZ中断频率

#define Udc 13.0f
#define Pole 7

typedef enum {
    CTRL_MODE_OPENLOOP = 0,
    CTRL_MODE_VOLTAGE,
    CTRL_MODE_TORQUE,
    CTRL_MODE_SPEED,
    CTRL_MODE_POSITION,
    // future: CTRL_MODE_CURRENT, etc.
} control_mode_t;

typedef enum {
    RUNSTATE_STOPPED = 0,
    RUNSTATE_STARTING,    // ramp up
    RUNSTATE_RUNNING,     // steady
    RUNSTATE_BRAKING,     // ramp down
    RUNSTATE_STALLED,     // detected stall
    RUNSTATE_CALIBRATING, // e.g. voltage calibration
    RUNSTATE_LOCKED,      // locked state
} run_state_t;

typedef enum {
    DIR_UNKNOWN = 0,
    DIR_CW,
    DIR_CCW,
} direction_t;

/* 故障位掩码（可扩展）*/
typedef enum {
    FAULT_NONE         = 0,
    FAULT_OVERVOLTAGE  = 1<<0,
    FAULT_UNDERVOLTAGE = 1<<1,
    FAULT_OVERCURRENT  = 1<<2,
    FAULT_STALL        = 1<<3,
    FAULT_SENSOR       = 1<<4,
    // ...
} fault_bits_t;

/* 一些运行标志 */
typedef struct {
    uint8_t calibrated : 1;
    uint8_t homed      : 1;
    uint8_t servo_on   : 1;
    uint8_t reserved   : 5;
} run_flags_t;

/* 主状态结构体 */
typedef struct {
    control_mode_t ctrl_mode;   // 控制模式：速度/扭矩/位置/电压...
    run_state_t    run_state;   // 运行子态：启动/运行/停止/堵转...
    direction_t    direction;   // 方向
    run_flags_t    flags;       // 小标志位
    uint32_t       faults;      // 故障位掩码
    // 可附带上下文参数：
//    float          target;      // 目标值（rpm/torque/position/voltage）
//    float          actual;      // 实时量
} MotorState_t;
extern MotorState_t MotorState;

typedef struct{
	 
   int32_t  VBUS;	      //电机母线电压
	 int32_t  Current_Rating;	      //电机额定电流
	 int32_t  Phase_L;    //相电感
	 int32_t  Phase_R;    //相电阻
	 int32_t  Flux;       //磁链
	 int32_t  Pole_Pairs;  //极对数
	
	 int32_t  Rotor_inertia;  //转动惯量
}Motor_Params_t;
extern Motor_Params_t Motor_Params;
#define Motor_Params_DEFAULTS  {24,3,0,0,0,0,0}

extern stMcInfo mc_info;
extern volatile int16_t comm[6];
extern  float time_us;
extern  void (* func_ptr)(ADC_HandleTypeDef *hadc);


void Svpwm_Outpwm(void);
uint32_t IF_id_RUN(uint16_t eleangle_step, uint16_t Id_ref);
uint32_t IF_RUN(uint16_t RPM_ref, uint16_t Iq_ref, uint16_t Time_ms);
uint32_t VF_RUN(uint16_t RPM_ref, uint32_t Uq_ref, uint16_t Time_ms);
float VF_RUN_Float(float RPM_ref, float Uq_ref, float Time_ms);
#endif
